import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteSetupExtend from 'vite-plugin-vue-setup-extend'
import IconsResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'
import path from "node:path";

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
    build: {
      watch: {}
    }
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    resolve: {
      alias: {
        '@': resolve('src/renderer/src')
      }
    },
    plugins: [
      vue(),
      viteSetupExtend(),
      AutoImport({
        // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
        imports: ['vue','vue-router','pinia'],
        resolvers: [
          ElementPlusResolver(),
          // 自动导入图标组件 https://icon-sets.iconify.design/bi/  (替换url后缀即可)
          IconsResolver({
            prefix: 'Icon',
            enabledCollections: ['bi','svg']
          })
        ],
        dts: path.resolve(__dirname,"src/renderer/auto-imports.d.ts")
      }),
      Components({
        // allow auto load markdown components under `./src/components/`
        extensions: ['vue', 'md','element-plus'],
        // 允许自动导入和注册markdown中使用的组件
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        resolvers: [
          ElementPlusResolver(),
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ['bi','svg']
          })
        ],
        dts: path.resolve(__dirname,"src/renderer/components.d.ts")
      }),
      Icons({
        autoInstall: true
      }),
    ],
    //后台服务
    server: {
      proxy: {
        //自定义后端服务
        '/p2': {
          target: 'http://p2.music.126.net',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/p2/, ''),
        },
        '/p1': {
          target: 'http://p1.music.126.net',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/p1/, ''),
        },
        //网易官网接口
        '/music': {
          target: 'http://111.180.193.222:3000',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/music/, ''),
        },
        //网易官网接口
        '/play': {
          target: 'http://m801.music.126.net',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/play/, ''),
        },
      },
      hmr: true
    },
  },
})
